package com.nuoniu.sibanyun.service.cms.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.cms.CmsBanner;
import com.nuoniu.sibanyun.entity.cms.CmsColumn;
import com.nuoniu.sibanyun.mapper.cms.CmsColumnMapper;
import com.nuoniu.sibanyun.service.cms.ICmsColumnService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网站栏目 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-08-10
 */
@Service
public class CmsColumnServiceImpl extends ServiceImpl<CmsColumnMapper, CmsColumn> implements ICmsColumnService {

    @Override
    public Page<CmsColumn> pageList(Page page, CmsColumn column) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        if(column.getType() != null) {
            wrapper.eq("type", column.getType());
        }

        wrapper.eq("company_id", userInfo.getCompanyId());
        return page(page, wrapper);
    }

    @Override
    public void saveColumn(CmsColumn column) {
        UserInfo userInfo = UserThreadLocal.get();
        column.setCompanyId(userInfo.getCompanyId());
        this.saveOrUpdate(column);
    }

    @Override
    public void deleteById(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id", id);
        Integer count = this.count(wrapper);
        if(count > 0) {
            throw new NuoNiuException("有子级栏目,不能删除");
        }

        this.removeById(id);
    }
}
